package top.ninwoo.cloudcenter.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.cloudcenter.service.CloudService;
import top.ninwoo.common.entity.ContainerMonitorInfo;
import top.ninwoo.common.entity.NetworkTopology;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/network")
public class NetworkController {
    @Resource
    CloudService cloudService;

    @RequestMapping(value = "/getLogicalNetworkTopology")
    public NetworkTopology getLogicalNetworkTopology(Long clusterId) {
        return cloudService.getLogicalNetworkByClusterId(clusterId);
    }

    @RequestMapping(value = "/getIpListByAppName")
    public List<String> getIpListByAppName(Long clusterId, String appName) {
        Set<String> ipSet = cloudService.getIpListFromRemoteIpServiceByAppName(clusterId, appName);
        // 这里的ipSet是无序的
        return new ArrayList<>(ipSet);
    }

    @RequestMapping(value = "/enableNetworkMonitor")
    public String enableNetworkMonitor(Long clusterId, String appName) {
        return cloudService.enableNetworkMonitor(clusterId, appName);
    }

    @RequestMapping(value = "/cancelNetworkMonitor")
    public String cancelNetworkMonitor(Long clusterId, String appName) {
        return cloudService.cancelNetworkMonitor(clusterId, appName);
    }

    @RequestMapping(value = "/getNetworkMonitorInfo")
    public List<ContainerMonitorInfo> getNetworkMonitor(Long clusterId, String appName) {
        // 这里可以理解为是返回一个网络监控的钩子程序，具体的查询还需要通过钩子进行执行
        return cloudService.getContainerMonitorInfoByAppName(clusterId, appName);
    }

    @RequestMapping(value = "/addQos")
    public Boolean addQos(Long clusterId, String appName, String maxRate, String latency) {
        return cloudService.addQos(clusterId, appName, maxRate, latency);
    }
}
